/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.pi;

import java.awt.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.LoggedData.LoggedData;
import researchgrants.parts.db.Db;
import researchgrants.parts.pi.departments.Department;

/**
 *
 * @author ohad
 */
public class Pi {

    private int id;
    private LoggedData name, telephone, email, notes, department;

    public static Pi getById(int id) {
        return(new Pi(id));
    }
    
    public static Pi inputNew(Window parent) {
        PiView newPiView=new PiView(parent);
        return(newPiView.getPi());
    }

    private Pi(int id) {
        this.id=id;
//        syncDataFromDb();
    }

   private void syncDataFromDb() {
        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement stat = Db.createPreparedStatement(conn, "SELECT NameRef, TelephoneRef, EmailRef, DepartmentRef, NotesRef FROM tblPIs Where PiID=?");
            stat.setInt(1, id);
            stat.executeQuery();
            ResultSet rs=stat.getResultSet();
            Db.moveToFirstLine(rs);

            name=LoggedData.getByID((Integer) Db.getColumn(rs, 1));
            telephone=LoggedData.getByID((Integer) Db.getColumn(rs, 2));
            email=LoggedData.getByID((Integer) Db.getColumn(rs, 3));
            department=LoggedData.getByID((Integer) Db.getColumn(rs, 4));
            notes=LoggedData.getByID((Integer) Db.getColumn(rs, 5));
            
            rs.close();
            stat.close();
            Db.closeDbConnection(conn);
            
        } catch (SQLException ex) {
            Logger.getLogger(LoggedData.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            throw new RuntimeException("Problem with Data: " + ex.getLocalizedMessage());
        }
    }
    
    public String getShortForm() {
        syncDataFromDb();
        return(name.value());
    }

    public String getLongForm() {
        syncDataFromDb();
        return(name.value() + "(#" + telephone.value() + ", @" + email.value() + ")");
    }
    
    public void show(Window parent) {
        new PiView(parent, this);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Pi other = (Pi) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + this.id;
        return hash;
    }

    public int getId() {
        return(id);
    }
    
    public String getCurrentName() {
        syncDataFromDb();
        return(name.value());
    }

    public String getCurrentTelephone() {
        syncDataFromDb();
        return(telephone.value());
    }

    public String getCurrentEmail() {
        syncDataFromDb();
        return(email.value());
    }
    
    public String getCurrentNotes() {
        syncDataFromDb();
        return(notes.value());
    }
    
    public Department getCurrentDepartment() {
        syncDataFromDb();
        return(Department.getByDepartmentName(department.value()));
    }

    LoggedData getLoggedDataDepartment() {
        syncDataFromDb();
        return(department);
    }

    LoggedData getLoggedDataEmail() {
        syncDataFromDb();
        return(email);
    }

    LoggedData getLoggedDataName() {
        syncDataFromDb();
        return(name);
    }

    LoggedData getLoggedDataNotes() {
        syncDataFromDb();
        return(notes);
    }

    LoggedData getLoggedDataTelephone() {
        syncDataFromDb();
        return(telephone);
    }

}
